package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.config.CommonPropertiesConfig;
import com.ytjj.qmyx.mall.model.response.ActivityChinaGlodIndexResponse;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * h5 活动
 *
 **/
@RestController
@RequestMapping("/h5/activity")
public class CommonActivityController extends BaseController {
    @Autowired
    private ActivityService activityService;
    @Resource(name = "mallCommonPropertiesConfig")
    private CommonPropertiesConfig commonPropertiesConfig;

    /**
     *
     * @param activityType 活动类型
     * @param columnIds    活动栏目，多个用','分隔
     * @param pageSizes    每个columnId查询条数，多个用','分隔(同上面的columnIds一一对应)
     * @param flag         是否乱序 1-乱序 其它不乱序
     * @return             返回map格式 比如columnIds="1,2,3" 则返回的key分别为：key1、key2、key3
     */
    @GetMapping("index")
    public CommonResult index (@RequestParam("activityType") String activityType,
                               @RequestParam(value = "columnIds", required = false) String columnIds,
                               @RequestParam(value = "pageSizes", required = false) String pageSizes,
                               @RequestParam(value = "flag", required = false) Integer flag) {
        if (StringUtils.isEmpty(activityType)) {
            throw new ApiException("参数错误！");
        }
        if (StringUtils.isEmpty(columnIds)) {
            throw new ApiException("活动栏目不能为空！");
        }
        String[] pageSizeArr = null;
        if (!StringUtils.isEmpty(pageSizes)) {
            pageSizeArr = pageSizes.split(",");
        }
        Map<String, Object> result = new HashMap();
        AtomicInteger index = new AtomicInteger(0);
        String[] finalPageSizeArr = pageSizeArr;
        Arrays.stream(columnIds.split(",")).forEach(item -> {
            int pageSize = 20;
            int key_index = index.get();
            String key = "key" + (key_index + 1);
            if (null != finalPageSizeArr && finalPageSizeArr.length >= index.get()) {
                pageSize = Integer.parseInt(finalPageSizeArr[index.getAndAdd(1)]);
            }
            List<ActivityProductResponse> productList = activityService.getActivityList(1,
                    pageSize <= 0 ? 20 : pageSize,
                    activityType,
                    Integer.parseInt(item));
            if (1 == flag) {
                Collections.shuffle(productList);
            }
            result.put(key, productList);
        });
        return CommonResult.success(result);
    }

    /**
     * 一分兑活动
     * @param activityType
     * @param columnId
     * @param pageNum
     * @param pageSize
     * @param flag
     * @return
     */
    @GetMapping("/retroactiveCard/index")
    public CommonResult retroactiveCardIndex (@RequestParam("activityType") String activityType,
                               @RequestParam(value = "columnId", required = false) Integer columnId,
                               @RequestParam(value = "pageNum", required = false) Integer pageNum,
                               @RequestParam(value = "pageSize", required = false) Integer pageSize,
                               @RequestParam(value = "flag", required = false) Integer flag) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        ActivityChinaGlodIndexResponse chinaGlodIndexResponse=new ActivityChinaGlodIndexResponse();
        List<ActivityProductResponse> productList = activityService.getActivityList(pageNum, pageSize, activityType, columnId);
        if (1 == flag) {
            Collections.shuffle(productList);
        }
        chinaGlodIndexResponse.setFirstProductList(productList);
        return CommonResult.success(chinaGlodIndexResponse);
    }

    /**
     * 栏目商品列表
     * @param activityType 活动类型
     * @param columnId      活动栏目id
     * @param pageSize      查询多少条
     * @param pageNum       第多少页
     * @param flag          是否需要打乱顺序 1-打乱
     * @param mark          是否需要根据ip查商品 1-按照ip查商品
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "activityType") String activityType,
                                        @RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                        @RequestParam(value = "mark", required = false, defaultValue = "0") Integer mark) {
        List<ActivityProductResponse> productList = new ArrayList<>();
        if(mark != null && 1 == mark){
            productList = activityService.getActivityListByArea(pageNum, pageSize, activityType, columnId);
        }else {
            productList = activityService.getActivityList(pageNum, pageSize, activityType, columnId);
        }
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        //app的品打乱顺序
//        if(TokenUtil.getPlatformData().equals("app") && TokenUtil.getDomainUrl().equals("https://vvip.gucmall.cn")){
//            Collections.shuffle(productList);
//        }
        return CommonResult.success(productList);
    }

    /**
     * 根据栏目id获取栏目名称
     * @return
     */
    @GetMapping("/getColumnName")
    public CommonResult getColumnName(Integer columnId){
        return CommonResult.success(activityService.getColumnName(columnId));
    }


    /**
     * 栏目商品列表
     * @param activityType 活动类型
     * @param columnIds      活动栏目id(用','分割)
     * @param pageSize      查询多少条
     * @param pageNum       第多少页
     * @param flag          是否需要打乱顺序 1-打乱
     * @return
     */
    @GetMapping("/queryListByColumnIds")
    public CommonResult queryListByColumnIds(@RequestParam(value = "activityType") String activityType,
                                        @RequestParam(value = "columnIds", required = false, defaultValue = "0") String columnIds,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {

        List<ActivityProductResponse> productList = activityService.getActivityListByColumnIds(pageNum, pageSize, activityType, columnIds);
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        return CommonResult.success(productList);
    }

    /**
     * 猜你喜欢
     * @return
     */
    @GetMapping("/recommendProduct")
    public CommonResult recommendProduct(Integer productId,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum){
        List<ActivityProductResponse> productList = activityService.recommendProduct(productId,pageNum,pageSize);
        return CommonResult.success(productList);
    }
}
